 #利用map批量检测用户名是否符合规范=> username符合规范, username不符合规范
#只能包含数据、字母、下划线，不以数字开头，长度在6-18之间

names = ['123','12dcghj','zhai-wenxiu','huang_xinyi','agsjkh12','zs23']
def f_match(x):
    import re
    if re.findall("^[^0-9][A-Za-z0-9_]{6,18}",x):
        return(f"{x}符合规范")
    else:
        return(f"{x}不符合规范")
print(list(map(f_match,names)))

#filter把合法的用户名显示出来
def f_match1(x):
    import re
    if re.findall("^[^0-9][A-Za-z0-9_]{6,18}",x):
        return(x)
print(list(filter(f_match1,names)))

# 匿名函数+reduce实现 1*2*3*...*n
from functools import reduce
num = int(input("请输入一个数:"))
result = reduce(lambda x,y:x*y,range(1,num+1))
print(f"{num}的阶乘为",result)

# filter函数:找出[1-100]能被3整除的数
result1 = filter(lambda x :x%3 == 0,range(1,101))
print(list(result1))

#map函数:求list[1-10]各项的平方
result2 = map(lambda x:x*x,range(1,11))
print(list(result2))

# 将列表[1,4,5,9] 转换成 '1459'的字符串
a = [1,4,5,9]
from functools import reduce
def f(x,y):
    return x*10+y
result3 = reduce(f,a)
print(str(result3))
print(type(str(result3)))

#实现“123.456” --> 123.456  str --》 float
s = "123.456"
def f1(x,y):
    return x*10+y
n = s.index('.')
s1 = [i for i in s[:n]]
s2 = [i for i in s[n+1:]]
def f2(x):
    return int(x)
num1 = reduce(f1,map(f2,s1))
num2 = reduce(f1,map(f2,s2))
print(num1+num2/10**len(s2))

#有以下列表：list1=[7, -8, 5, 4, 0, -2, -5]    • 正数在前负数在后 • 整数从小到大 • 负数从大到小

list1=[7, -8, 5, 4, 0, -2, -5]
list3 = sorted([i for i in list1 if i>=0])+sorted([i for i in list1 if i<0],reverse=True)
print(list3)


#s = 'asdf234GDSdsf23' 排序:小写-大写-奇数-偶数 • 原理：先比较元组的第一个值，FALSE

s = 'a23156sdf234dsWWSSGDSdsf23'

#print(*(sorted(s,key = lambda x:(x.isdigit() and int(x)%2==0, x.isdigit(),x.isupper(),x.islower(),x))),sep='')

result2 = sorted(s,key = lambda x :(x.isdigit() and int(x)%2 == 0,x.isdigit(),x.isupper(),x))
result3 = ("".join(i for i in result2))
print(result3)


